package com.google.code.p.restrpc.core.async.mem;

import java.util.Properties;

import org.apache.commons.lang.StringUtils;

/**
 * @author dz
 */
public class AsyncRpcResultMemCacheOptions {
    
    public static final String DETECT_WORKER = "restrpc.async.mem.cache.detect.worker";
    
    public static final String DETECT_FREQUENCy = "restrpc.async.mem.cache.detect.frequency";
    
    public static final String IDLE_TIMEOUT = "restrpc.async.mem.cache.idle.timeout";
    
    public static final String MAX_SIZE = "restrpc.async.mem.cache.max.size";
    
    private int worker = 1;
    
    private int detectFrequency = 60; // Time Unit : Second
    
    private int idleTimeout = 5 * 60; // Time Unit : Second
    
    private int maxSize = 10000; // max entry size
    
    public AsyncRpcResultMemCacheOptions(Properties properties) {
        if (properties != null) {
            initProperties(properties);
        }
    }
    
    private void initProperties(Properties properties) {
        int worker = getWorker(properties);
        if (worker > 0) {
            this.worker = worker;
        }
        
        int detectFrequency = getDetectFrequency(properties);
        if (detectFrequency > 0) {
            this.detectFrequency = detectFrequency;
        }
        
        int idleTimeout = getIdleTimeout(properties);
        if (idleTimeout > 0) {
            this.idleTimeout = idleTimeout;
        }
        
        int maxSize = getMaxSize(properties);
        if (maxSize > 0) {
            this.maxSize = maxSize;
        }
    }
    
    public static int getWorker(Properties properties) {
        String workerStr = properties.getProperty(DETECT_WORKER);
        if (StringUtils.isEmpty(workerStr)) {
            return -1;
        }
        return Integer.parseInt(workerStr);
    }
    
    public static int getMaxSize(Properties properties) {
        String detectFrequencyStr = properties.getProperty(DETECT_FREQUENCy);
        if (StringUtils.isEmpty(detectFrequencyStr)) {
            return -1;
        }
        return Integer.parseInt(detectFrequencyStr);
    }
    
    public static int getIdleTimeout(Properties properties) {
        String idleTimeoutStr = properties.getProperty(IDLE_TIMEOUT);
        if (StringUtils.isEmpty(idleTimeoutStr)) {
            return -1;
        }
        return Integer.parseInt(idleTimeoutStr);
    }
    
    public static int getDetectFrequency(Properties properties) {
        String maxSizeStr = properties.getProperty(MAX_SIZE);
        if (StringUtils.isEmpty(maxSizeStr)) {
            return -1;
        }
        return Integer.parseInt(maxSizeStr);
    }
    
    /**
     * @return the worker
     */
    public int getWorker() {
        return worker;
    }
    
    /**
     * @return the detectFrequency
     */
    public int getDetectFrequency() {
        return detectFrequency;
    }
    
    /**
     * @return the idleTimeout
     */
    public int getIdleTimeout() {
        return idleTimeout;
    }
    
    /**
     * @return the maxSize
     */
    public int getMaxSize() {
        return maxSize;
    }
    
}
